<?php

class DATABASE {

  public $database;
  public $tables = array();
  public $query;

  function __construct() {
    try {
      /* create or open the database */
      $this->database = new SQLiteDatabase('db/luma.db3', 0666, $error);

      /* create master table*/
      $query = "SELECT type, name FROM sqlite_master WHERE type='table' ORDER BY name";
      $result1 = $this->database->arrayQuery($query, SQLITE_ASSOC);
      //load all tables into master tables
      $table = array();
      for ($i = 0; $i < sizeof($result1); $i++) {
        $query = "PRAGMA table_info('".$result1[$i]['name']."')";
        $result2 =  $this->database->arrayQuery($query, SQLITE_ASSOC);

        for ($j = 0; $j < sizeof($result2); $j++) {
          $table[] = $result2[$j]['name'];
        }
        $this->tables[$result1[$i]['name']] = $table;
        $table = array();
      }
      //load operators into master tables
      //oper[idx] =   0      1      2       3      4       5       6         7             8        9
      $oper = array(" = ", " > ", " >= ", " < ", " <= ", " != ", " LIKE ", " NOT LIKE ", " AND ", " OR ");
      $this->tables['operator'] = $oper;
    } catch(Exception $e) {
      die($error);
    }
  }

  public function db_insert($table_name, $table_param_list) {

    unset($this->query);

    $this->query  = "INSERT INTO ".
    $this->query .= $table_name;
    $this->query .= " (";
    for ($i = 0; $i < sizeof($table_param_list[0]); $i++) {
      if ($i == 0) {
        $this->query .= $this->tables[$table_name][$table_param_list[0][$i]];
      } else {
        $this->query .= ",";
        $this->query .= $this->tables[$table_name][$table_param_list[0][$i]];
      }
    }
    $this->query .= ") VALUES (";
    for ($i = 0; $i < sizeof($table_param_list[1]); $i++) {
      if ($i == 0) {
        $this->query .= "'";
        $this->query .= $table_param_list[1][$i];
        $this->query .= "'";
      } else {
        $this->query .= ",";
        $this->query .= "'";
        $this->query .= $table_param_list[1][$i];
        $this->query .= "'";
      }
    }
    $this->query .= ")";

    return $this->query;
  }

  public function db_update($table_name, $table_param_list, $table_condition) {

    unset($this->query);

    $this->query = "UPDATE ";
    $this->query .= $table_name;
    $this->query .= " SET ";
    for ($i = 0; $i < sizeof($table_param_list[0]); $i++) {
      if ($i == 0) {
        $this->query .= $this->tables[$table_name][$table_param_list[0][$i]];
        $this->query .= "=";
        $this->query .= "'";
        $this->query .= $table_param_list[1][$i];
        $this->query .= "'";
      } else {
        $this->query .= ",";
        $this->query .= $this->tables[$table_name][$table_param_list[0][$i]];
        $this->query .= "=";
        $this->query .= "'";
        $this->query .= $table_param_list[1][$i];
        $this->query .= "'";
      }
    }
    $this->query .= " WHERE ";
    for ($i = 0; $i < sizeof($table_condition[0]); $i++) {
      if ($i == 0) {
        $this->query .= $this->tables[$table_name][$table_condition[0][$i]];
        $this->query .= $this->tables['operator'][$table_condition[1][$i]];
        $this->query .= "'";
        $this->query .= $table_condition[2][$i];
        $this->query .= "'";
      } else {
        if (!isset($table_condition[3])) {
          $this->query .= " AND ";
        } else {
          $this->query .= $this->tables['operator'][$table_condition[3][$i-1]];
        }
        $this->query .= $this->tables[$table_name][$table_condition[0][$i]];
        $this->query .= $this->tables['operator'][$table_condition[1][$i]];
        $this->query .= "'";
        $this->query .= $table_condition[2][$i];
        $this->query .= "'";

      }
    }

    return $this->query;
  }

  public function db_select($table_name, $table_col_name, $table_condition) {

    unset($this->query);

    $this->query = "SELECT ";
    if ($table_col_name == NULL) {
      $this->query .= "*";
    } else if ($table_col_name == "count") {
      $this->query .= "count(*)";
    } else {
      for ($i = 0; $i < sizeof($table_col_name); $i++) {
        if ($i == 0) {
          $this->query .= $this->tables[$table_name][$table_col_name[$i]];
        } else {
          $this->query .= ",";
          $this->query .= $this->tables[$table_name][$table_col_name[$i]];
        }
      }
    }
    $this->query .= " FROM ";
    $this->query .= $table_name;
    if ($table_condition != NULL) {
      $this->query .= " WHERE ";
      for ($i = 0; $i < sizeof($table_condition[0]); $i++) {
        if ($i == 0) {
          $this->query .= $this->tables[$table_name][$table_condition[0][$i]];
          $this->query .= $this->tables['operator'][$table_condition[1][$i]];
          $this->query .= "'";
          $this->query .= $table_condition[2][$i];
          $this->query .= "'";
        } else {
          if (!isset($table_condition[3])) {
            $this->query .= " AND ";
          } else {
            $this->query .= $this->tables['operator'][$table_condition[3][$i-1]];
          }
          $this->query .= $this->tables[$table_name][$table_condition[0][$i]];
          $this->query .= $this->tables['operator'][$table_condition[1][$i]];
          $this->query .= "'";
          $this->query .= $table_condition[2][$i];
          $this->query .= "'";
        }
      }
    }

    return $this->query;
  }

  public function db_delete($table_name, $table_condition) {

    unset($this->query);

    $this->query = "DELETE FROM ";
    $this->query .= $table_name;
    $this->query .= " WHERE ";
    for ($i = 0; $i < sizeof($table_condition[0]); $i++) {
      if ($i == 0) {
        $this->query .= $this->tables[$table_name][$table_condition[0][$i]];
        $this->query .= $this->tables['operator'][$table_condition[1][$i]];
        $this->query .= "'";
        $this->query .= $table_condition[2][$i];
        $this->query .= "'";
      } else {
        if (!isset($table_condition[3])) {
          $this->query .= " AND ";
        } else {
          $this->query .= $this->tables['operator'][$table_condition[3][$i-1]];
        }
        $this->query .= $this->tables[$table_name][$table_condition[0][$i]];
        $this->query .= $this->tables['operator'][$table_condition[1][$i]];
        $this->query .= "'";
        $this->query .= $table_condition[2][$i];
        $this->query .= "'";

      }
    }

    return $this->query;
  }

  function __destruct() {
    $this->database = NULL;
  }

}
/*
$db = new DATABASE();
print_r($db->tables);

$params = array();
$param1 = array(1, 2, 3, 4);
$param2 = array("nazrul", "hisham", "mnhisham@gmail.com", "nazrul1");
$params[] = $param1;
$params[] = $param2;
print_r($db->db_insert("user", $params));
echo "\n";

$params = array();
$param1 = array(1, 2);
$param2 = array("logo", "image logo");
$params[] = $param1;
$params[] = $param2;
print_r($db->db_insert("picture", $params));
echo "\n";

$params = array();
$param1 = array(1, 2, 3, 4);
$param2 = array("Nazrul", "Hisham", "mnhisham@gmail.com", "nazrul1");
$params[] = $param1;
$params[] = $param2;
$conds = array();
$param1 = array(0, 2);
$param2 = array(2, 6);
$param3 = array("3", "%is%m");
$conds[] = $param1;
$conds[] = $param2;
$conds[] = $param3;
print_r($db->db_update("user", $params, $conds));
echo "\n";

$params = array();
$param1 = array(1, 2, 3, 4);
$param2 = array("Salmi", "Syahirah", "salmi.syahirah@gmail.com", "meami123");
$params[] = $param1;
$params[] = $param2;
$conds = array();
$param1 = array(0);
$param2 = array(0);
$param3 = array("5");
$conds[] = $param1;
$conds[] = $param2;
$conds[] = $param3;
print_r($db->db_update("user", $params, $conds));
echo "\n";

$cols = array(0, 2, 3, 4);
$conds = array();
$param1 = array(0, 2);
$param2 = array(2, 6);
$param3 = array("3", "%is%m");
$conds[] = $param1;
$conds[] = $param2;
$conds[] = $param3;
print_r($db->db_select("user", $cols, $conds));
echo "\n";

$cols = array(1);
$conds = array();
$param1 = array(0);
$param2 = array(0);
$param3 = array("3");
$conds[] = $param1;
$conds[] = $param2;
$conds[] = $param3;
print_r($db->db_select("user", $cols, $conds));
echo "\n";

print_r($db->db_select("user", NULL, NULL));
echo "\n";
*/
class ADDRESS {

	public $address_id;
	public $address_fullname;
	public $address_line1;
	public $address_line2;
	public $address_city;
	public $address_region;
	public $address_postcode;
	public $address_country;
	public $address_phone;

  function __construct( $id = 0
                      , $fullname
                      , $addline1
                      , $addline2
                      , $addcity
                      , $addregion
                      , $addpostcode
                      , $addcountry
                      , $addphone
                      )
  {
    $this->address_id       = $id;
    $this->address_fullname = $fullname;
    $this->address_line1    = $addline1;
    $this->address_line2    = $addline2;
    $this->address_city     = $addcity;
    $this->address_region   = $addregion;
    $this->address_postcode = $addpostcode;
    $this->address_country  = $addcountry;
    $this->address_phone    = $addphone;
  }

  function __destruct() {
    //do nothing
  }
}

class CUSTOMER {

	public $customer_id;
	public $customer_email;
	public $customer_loginby;
	public $address_id;

  function __construct( $cid = 0
                      , $email
                      , $loginby
                      , $aid = 0
                      )
  {
    $this->customer_id      = $cid;
    $this->customer_email   = $email;
    $this->customer_loginby = $loginby;
    $this->address_id       = $aid;
  }

  function __destruct() {
    //do nothing
  }
}
?>
